using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class CombatInfoPanel : MonoBehaviour {
	
	public UILabel DR;
	public UILabel HP;
	public UILabel AC;
	public UILabel SR;
	public UILabel hpStatus;
	
	public GameObject ACBox;
	public GameObject HPBox;
	public GameObject SRBox;
	public GameObject DRBox;
	
	public BuffsListDisplay buffList;
	public WeaponListDisplay weaponList;
	public ProtectiveItemListDisplay pItemList;

	// Use this for initialization
	void Start () {
		UIButtonMessage message = ACBox.AddComponent<UIButtonMessage>();
		message.target = gameObject;
		message.functionName = "ACClicked";
		
		message = HPBox.AddComponent<UIButtonMessage>();
		message.target = gameObject;
		message.functionName = "HealthClicked";
		
		message = SRBox.AddComponent<UIButtonMessage>();
		message.target = gameObject;
		message.functionName = "SRClicked";
		
		message = DRBox.AddComponent<UIButtonMessage>();
		message.target = gameObject;
		message.functionName = "DRClicked";
		
		ReloadDisplays();
	}
	
	// Update is called once per frame
	void Update () {
		if(SheetManager.activeCharacterUpdated)
		{
			SheetManager.activeCharacterUpdated = false;
			
			if(pItemList.toggled)
			{
				pItemList.ToggleProtectiveDisplays();
				pItemList.ToggleProtectiveDisplays();
			}
			if(weaponList.toggled)
			{
				weaponList.ToggleWeaponDisplays();
				weaponList.ToggleWeaponDisplays();
			}
			if(buffList.toggled)
			{
				buffList.ToggleWindow();
				buffList.ToggleWindow();
			}
			
			ReloadDisplays();
		}
	}
	
	void ReloadDisplays()
	{
		AC.text = SheetManager.activeCharacter.AC.ToString();
		DR.text = SheetManager.activeCharacter.extraInfo.DR + "/" + SheetManager.activeCharacter.extraInfo.DRMaterial;
		SR.text = SheetManager.activeCharacter.extraInfo.SpellResistance.ToString();
		HP.text = (SheetManager.activeCharacter.HP-SheetManager.activeCharacter.Damage) + "/" + (SheetManager.activeCharacter.HP);
		hpStatus.text = SheetManager.activeCharacter.hpStatus;
	}
	
	IEnumerator HealthClicked()
	{
		yield return StartCoroutine(HelperFunctions.HealthWindow(SheetManager.activeCharacter));
		HP.text = (SheetManager.activeCharacter.HP-SheetManager.activeCharacter.Damage) + "/" + (SheetManager.activeCharacter.HP);
		hpStatus.text = SheetManager.activeCharacter.hpStatus;
		SheetManager.SaveActiveCharacter();
	}
	
	void ACClicked()
	{
		string acDescription = "AC: (" + SheetManager.activeCharacter.AC + ") = 10 + " + SheetManager.activeCharacter.DexterityModifier + " ";
		
		foreach(var acMod in SheetManager.activeCharacter.GetBuffMods("AC").SubTotals())
		{
			acDescription += "+ " + acMod.Value + "[" + acMod.Key + "] ";
		}
		
		StartCoroutine(HelperFunctions.ConfirmationWindow(acDescription, null));
	}
	
	IEnumerator DRClicked()
	{
		List<SetterWindowDescription> updates = new List<SetterWindowDescription>
			{
				new SetterWindowDescription("DR", SheetManager.activeCharacter.extraInfo.DR.ToString(), false),
				new SetterWindowDescription("Type", SheetManager.activeCharacter.extraInfo.DRMaterial, true)
			};
		
		yield return StartCoroutine(HelperFunctions.SetterWindow( "Change Damage Reduction?",updates));
		
		bool accepted = SheetManager.activeCharacter.extraInfo.SetDR(updates[1].outVariable, updates[0].outVariable);
		if(accepted)
		{
			DR.text = SheetManager.activeCharacter.extraInfo.DR + "/" + SheetManager.activeCharacter.extraInfo.DRMaterial;
			SheetManager.SaveActiveCharacter();
		}
	}
	
	IEnumerator SRClicked()
	{
		List<SetterWindowDescription> updates = new List<SetterWindowDescription>
			{
				new SetterWindowDescription("S.R.", SheetManager.activeCharacter.extraInfo.SpellResistance.ToString(), false)
			};
		
		yield return StartCoroutine(HelperFunctions.SetterWindow( "Change Spell Resistance?",updates));
		
		bool accepted = SheetManager.activeCharacter.extraInfo.SetSR(updates[0].outVariable);
		if(accepted)
		{
			SR.text = SheetManager.activeCharacter.extraInfo.SpellResistance.ToString();
			SheetManager.SaveActiveCharacter();
		}
	}
}
